<?
/***
*    This file is part of Marketorama.
*
*    Marketorama is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Marketorama is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR *PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with Marketorama.  If not, see <http://www.gnu.org/licenses/>.
***/

/** 
*    class LoginManager 
*
*    This class is used as a controller responsible to handle the Login library, 
*    sending to it the data recollected from a form and redirecting the output to 
*     the appropiate page according to the success of it
*
**/
class LoginManager extends Controller 
{
    /**
    *    function index
    *
    *    This base function of the class loads a page with a login form
    *
    *    This should be probably removed and changed by a warning message like
    *    "The page you are trying to access needs you to login, blah, blah, blah"
    *
    **/
    function index()
    {
        $header_data['title']='Login';
        $header_data['current']='Login';
        $this->load->view('template/header', $header_data);
        $this->load->view('login_form');
        $this->load->view('template/footer');
    }
    
    /**
    *    Function validateCredentials
    *    
    *    Thakes the data from the appropiate form 
    **/
    function validateCredentials()
    {
        $username=$this->input->post('username');
        $password_md5= md5($this->input->post('password'));
        $remember_me=$this->input->post('autologin');
        $this->load->library('Login');
        $this->login->validateCredentials($username, $password_md5, $remember_me);
        if($this->login->isUserLoggedIn())
        {
			//load from config and redirect to the landing page after logout
			$landing_login=$this->config->item('landing_login');
			redirect(site_url($landing_login));
		} else
            $this->index();
        
    }
    /*
     * Function Logout
     * 
     * Calls the logout function from the login library and redirects to 
     * the logout landing page
     * 
     * returns nothing 
     * 
     */
    function logout()
    {
        $this->load->library('Login');
        $this->login->logout();
        //load from config and redirect to the landing page after logout
        $landing_logout=$this->config->item('landing_logout');
        redirect(site_url($landing_logout));

    }
}
?>
